#include "chatRoom.h"

ChatRoom::ChatRoom(const StdTcpSocketPtr &client) : m_clientInfo(client)
{
    /* 连接数据库 */
    m_sqliteDB.connectDB("./music.db");

    /* 创建数据库表 */
    const char *sql = "CREATE TABLE IF NOT EXISTS userInfo (username text not null,passwd text not null);";
    m_sqliteDB.executeSql(sql);
}

ChatRoom::~ChatRoom()
{
}

/* 判断用户是否存在 */
bool ChatRoom::userIsExist(const std::string(&name))
{
    std::string sql = "SELECT COUNT(1) from userInfo where username = '" + name + "';";
    QueryResult res = m_sqliteDB.querySql(sql);
    if (res.rows[0][0] == "0")
    {
        return false;
    }

    return true;
}

/* 保存用户信息 */
bool ChatRoom::saveUserInfo(const std::string &name, const std::string &passwd)
{
    /* 格式化字符串输入输出 */
    const char *sql = "INSERT INTO userInfo (username,passwd) VALUES('%s','%s');";
    char requestSql[BUFFER_SIZE] = {0};
    sprintf(requestSql, sql, name.c_str(), passwd.c_str());

    /* 执行sql语句 */
    return m_sqliteDB.executeSql(requestSql);
}

/* 处理用户注册 */
void ChatRoom::handleRegisterInfo(const std::string &msg)
{
    /* 1.将json字符串解析成字符串 */
    json_object *jsonObj = json_tokener_parse(msg.c_str());

    /* 2.根据key得到value. */
    std::string name = json_object_get_string(json_object_object_get(jsonObj, "name"));
    std::string passwd = json_object_get_string(json_object_object_get(jsonObj, "passwd"));

    /* 销毁json对象 */
    json_object_put(jsonObj);

    /* 创建json对象 */
    json_object *resObj = json_object_new_object();
    /* 2.向json对象添加<key,value>键值对 */
    json_object_object_add(jsonObj, "type", json_object_new_int(REGISTER));
    /* 先判断用户是否存在 */
    if (userIsExist(name))
    {
        std::cout << name << "用户已存在" << std::endl;
        json_object_object_add(jsonObj, "type", json_object_new_int(REGISTER_EXIST_USER));
    }
    else
    {
        /* 保存用户信息 */
        saveUserInfo(name, passwd);
        json_object_object_add(jsonObj, "type", json_object_new_int(REGISTER_SUCCESS));
        std::cout << "用户注册成功" << std::endl;
    }
    /* 将json对象转成json字符串 */
    const char *resStr = json_object_to_json_string(resObj);

    /* 发送消息回客户端... */
    m_clientInfo->sendMessage(resStr);
}

/* 处理用户登录*/
void ChatRoom::handleLoginInfo(const std::string &msg)
{
    /* 1.将json字符串解析成字符串 */
    json_object *jsonObj = json_tokener_parse(msg.c_str());

    /* 2.根据key得到value. */
    std::string name = json_object_get_string(json_object_object_get(jsonObj, "name"));
    std::string passwd = json_object_get_string(json_object_object_get(jsonObj, "passwd"));

    /* 销毁json对象 */
    json_object_put(jsonObj);

    /* 创建json对象 */
    json_object *resObj = json_object_new_object();
    /* 2.向json对象添加<key,value>键值对 */
    json_object_object_add(jsonObj, "type", json_object_new_int(LOGIN));

    // if (name == "zhangsan" && passwd == "456")
    // {
    //     json_object_object_add(resObj, "status", json_object_new_int(LOGIN_SUCCESS));
    // }
    // else
    // {
    //     json_object_object_add(resObj, "status", json_object_new_int(LOGIN_NOUSER));
    // }

    /* 将json对象转成json字符串 */
    const char *resStr = json_object_to_json_string(resObj);

    /* 发送消息回客户端... */
    m_clientInfo->sendMessage(resStr);
}
